<?php

namespace Home\Controller;

/**
 * 账目管理
 *
 * @author Administrator
 *        
 */
class AccountController extends BaseController {
	/**
	 * 总计账目
	 */
	public function mb_TotalAccount() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$userInfo = M ( 'Userinfo' );
		// $adminInfo = M ( 'Admininfo' );
		$userRechargeLog = M ( 'Userrechargelog' );
		$userBetLog = M ( 'Userbetlog' );
		$userWinLog = M ( 'Userwinlog' );
		$adminRechargeLog = M ( 'Adminrechargelog' );
		
		$name = $adminData ['name'];
		$code = $adminData ['code'];
		if ($adminData ['level'] == 0) {
			// 总管理员
			
			// 注册人数
			$userCount = $userInfo->count ();
			// 代理人数
			$agentCount = $adminInfo->where ( "level!='0' AND level!='1'" )->count ();
			// 总上
			// $userTotalUp = $userRechargeLog->where ( "type='0' AND handlercode = '$code'" )->sum ( 'coins' );
			$userTotalUp = $userRechargeLog->where ( "type='0'" )->sum ( 'coins' );
			// 总下
			// $userTotalDown = $userRechargeLog->where ( "type='1' AND handlercode = '$code'" )->sum ( 'coins' );
			$userTotalDown = $userRechargeLog->where ( "type='1'" )->sum ( 'coins' );
			// 总送
			// $userTotalSend = $userRechargeLog->where ( "type='2' AND handlercode = '$code'" )->sum ( 'coins' );
			$userTotalSend = $userRechargeLog->where ( "type='2'" )->sum ( 'coins' );
			
			$agentTotalUp = $adminRechargeLog->where ( "type='0' AND handlercode='1'" )->sum ( 'coins' );
			$agentTotalDown = $adminRechargeLog->where ( "type='1' AND handlercode='1'" )->sum ( 'coins' );
			$agentTotalSend = $adminRechargeLog->where ( "type='2' AND handlercode='1'" )->sum ( 'coins' );
			
			// 总押
			$totalBet = $userBetLog->sum ( 'bets' );
			// 总赢
			$totalWin = $userWinLog->sum ( 'wins' );
			// 玩家剩余分数
			$userCoins = $userInfo->sum ( 'coin' );
			// 代理分数
			$adminCoins = $adminInfo->sum ( 'coin' );
			
			if ($userTotalUp == '')
				$userTotalUp = 0;
			else
				$userTotalUp = round ( $userTotalUp, 3 );
			if ($userTotalDown == '')
				$userTotalDown = 0;
			else
				$userTotalDown = round ( $userTotalDown, 3 );
			if ($userTotalSend == '')
				$userTotalSend = 0;
			else
				$userTotalSend = round ( $userTotalSend, 3 );
			
			if ($agentTotalUp == '')
				$agentTotalUp = 0;
			else
				$agentTotalUp = round ( $agentTotalUp, 3 );
			if ($agentTotalDown == '')
				$agentTotalDown = 0;
			else
				$agentTotalDown = round ( $agentTotalDown, 3 );
			if ($agentTotalSend == '')
				$agentTotalSend = 0;
			else
				$agentTotalSend = round ( $agentTotalSend, 3 );
			
			if ($userCoins == '')
				$userCoins = 0;
			else
				$userCoins = round ( $userCoins, 3 );
			if ($adminCoins == '')
				$adminCoins = 0;
			else
				$adminCoins = round ( $adminCoins, 3 );
			if ($totalBet == '')
				$totalBet = 0;
			else
				$totalBet = round ( $totalBet, 3 );
			if ($totalWin == '')
				$totalWin = 0;
			else
				$totalWin = round ( $totalWin, 3 );
			
			// $totalUp = $userTotalUp + $agentTotalUp;
			// $totalDown = $userTotalDown + $agentTotalDown;
			// $totalSend = $userTotalSend + $agentTotalSend;
			
			$totalUp = $userTotalUp;
			$totalDown = $userTotalDown;
			$totalSend = $userTotalSend;
			
			// 盈利 总上-总下-代理-玩家
			// $profit = $totalUp + $totalDown - $adminCoins - $userCoins;
			$profit = $totalUp + $totalDown - $userCoins;
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$subCode = mb_substr ( $code, 0, 5 );
			$userCount = $userInfo->where ( "code like '" . $subCode . "___'" )->count ();
			$agentCount = $adminInfo->where ( "code like '" . $subCode . "___' AND level!='1' AND level = '3'" )->count ();
			// $totalUp = $userRechargeLog->where ( "handlercode like '" . $subCode . "___' AND type='0'" )->sum ( 'coins' );
			// $totalDown = $userRechargeLog->where ( "handlercode like '" . $subCode . "___' AND type='1'" )->sum ( 'coins' );
			// $totalSend = $userRechargeLog->where ( "handlercode like '" . $subCode . "___' AND type='2'" )->sum ( 'coins' );
			$userTotalUp = $userRechargeLog->where ( "code like '" . $subCode . "___' AND type='0'" )->sum ( 'coins' );
			$userTotalDown = $userRechargeLog->where ( "code like '" . $subCode . "___' AND type='1'" )->sum ( 'coins' );
			$userTotalSend = $userRechargeLog->where ( "code like '" . $subCode . "___' AND type='2'" )->sum ( 'coins' );
			
			$agentTotalUp = $adminRechargeLog->where ( "type='0' AND handlername='$name'" )->sum ( 'coins' );
			$agentTotalDown = $adminRechargeLog->where ( "type='1' AND handlername='$name'" )->sum ( 'coins' );
			$agentTotalSend = $adminRechargeLog->where ( "type='2' AND handlername='$name'" )->sum ( 'coins' );
			
			$userCoins = $userInfo->where ( "code like '" . $subCode . "___'" )->sum ( 'coin' );
			$adminCoins = $adminInfo->where ( "code like '" . $subCode . "___' AND code != '" . $code . "000' AND level = '3'" )->sum ( 'coin' );
			// 总押
			$totalBet = $userBetLog->where ( "code like '" . $subCode . "___'" )->sum ( 'bets' );
			// 总赢
			$totalWin = $userWinLog->where ( "code like '" . $subCode . "___'" )->sum ( 'wins' );
			
			if ($userTotalUp == '')
				$userTotalUp = 0;
			else
				$userTotalUp = round ( $userTotalUp, 2 );
			if ($userTotalDown == '')
				$userTotalDown = 0;
			else
				$userTotalDown = round ( $userTotalDown, 2 );
			if ($userTotalSend == '')
				$userTotalSend = 0;
			else
				$userTotalSend = round ( $userTotalSend, 2 );
			
			if ($agentTotalUp == '')
				$agentTotalUp = 0;
			else
				$agentTotalUp = round ( $agentTotalUp, 2 );
			if ($agentTotalDown == '')
				$agentTotalDown = 0;
			else
				$agentTotalDown = round ( $agentTotalDown, 2 );
			if ($agentTotalSend == '')
				$agentTotalSend = 0;
			else
				$agentTotalSend = round ( $agentTotalSend, 2 );
			
			if ($userCoins == '')
				$userCoins = 0;
			else
				$userCoins = round ( $userCoins, 2 );
			if ($adminCoins == '')
				$adminCoins = 0;
			else
				$adminCoins = round ( $adminCoins, 2 );
			if ($totalBet == '')
				$totalBet = 0;
			else
				$totalBet = round ( $totalBet, 2 );
			if ($totalWin == '')
				$totalWin = 0;
			else
				$totalWin = round ( $totalWin, 2 );
			
			$totalUp = $userTotalUp;
			$totalDown = $userTotalDown;
			$totalSend = $userTotalSend;
			
			$profit = $totalUp + $totalDown - $adminCoins - $userCoins;
			// $profit = $totalUp + $totalDown - $userCoins;
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$userCount = $userInfo->where ( "code = '$code'" )->count ();
			$agentCount = 0;
			$totalUp = $userRechargeLog->where ( "code ='$code ' AND type='0'" )->sum ( 'coins' );
			$totalDown = $userRechargeLog->where ( "code ='$code ' AND type='1'" )->sum ( 'coins' );
			$totalSend = $userRechargeLog->where ( "code ='$code ' AND type='2'" )->sum ( 'coins' );
			$userCoins = $userInfo->where ( "code = '$code'" )->sum ( 'coin' );
			// 总押
			$totalBet = $userBetLog->where ( "code = '$code'" )->sum ( 'bets' );
			// 总赢
			$totalWin = $userWinLog->where ( "code = '$code'" )->sum ( 'wins' );
			
			if ($totalUp == '')
				$totalUp = 0;
			else
				$totalUp = round ( $totalUp, 2 );
			if ($totalDown == '')
				$totalDown = 0;
			else
				$totalDown = round ( $totalDown, 2 );
			if ($totalSend == '')
				$totalSend = 0;
			else
				$totalSend = round ( $totalSend, 2 );
			if ($userCoins == '')
				$userCoins = 0;
			else
				$userCoins = round ( $userCoins, 2 );
			if ($totalBet == '')
				$totalBet = 0;
			else
				$totalBet = round ( $totalBet, 2 );
			if ($totalWin == '')
				$totalWin = 0;
			else
				$totalWin = round ( $totalWin, 2 );
			
			$adminCoins = 0;
			$profit = $totalUp + $totalDown - $userCoins;
			
			$agentTotalUp = 0;
			$agentTotalDown = 0;
			$agentTotalSend = 0;
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( array (
				'usercount' => ( string ) $userCount,
				'agentcount' => ( string ) $agentCount,
				'totalup' => ( string ) $totalUp,
				'totaldown' => ( string ) $totalDown,
				'totalsend' => ( string ) $totalSend,
				'usercoin' => ( string ) $userCoins,
				'profit' => ( string ) $profit,
				'agentcoin' => ( string ) $adminCoins,
				'totalbet' => ( string ) $totalBet,
				'totalwin' => ( string ) $totalWin,
				'agentup' => ( string ) $agentTotalUp,
				'agentdown' => ( string ) $agentTotalDown,
				'agentsend' => ( string ) $agentTotalSend 
		) ) );
	}
	/**
	 * 代理商账目
	 */
	public function mb_AgentAccount() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$type = $data->type;
		$agent = $data->agent;
		$date1 = $data->date1;
		$date2 = $data->date2;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$userBetLog = M ( 'Userbetlog' );
		$userWinLog = M ( 'Userwinlog' );
		$userRechargeLog = M ( 'Userrechargelog' );
		$userInfo = M ( 'Userinfo' );
		// if ($agent != '') {
		// // 按代理商查
		// $agentWhere = "code='$agent'";
		// $logWhere = '';
		// } else {
		// // 按时间查
		// $agentWhere = '';
		// $logWhere = "date>='$date1' AND date<='$date2'";
		// }
		
		if ($date1 == '' || $date2 == '')
			$logWhere = '';
		else
			$logWhere = "date>='$date1' AND date<='$date2'";
		
		if ($agent == '')
			$agentWhere = '';
		else
			$agentWhere = "code='$agent'";
		if ($adminData ['level'] == 0) {
			// 总管理员
			if ($agentWhere != '')
				$agentWhere .= ' AND ';
			if ($type == 0) {
				$agentDatas = $adminInfo->where ( $agentWhere . "level!='0' AND level!='1'" )->select ();
			} else {
				if ($adminData ['name'] == C ( 'ADMIN_NAME' ))
					$agentDatas = $adminInfo->where ( $agentWhere )->select ();
				else if ($adminData ['name'] == C ( 'DEVE_NAME' ))
					$agentDatas = $adminInfo->where ( $agentWhere . "name!='" . C ( 'ADMIN_NAME' ) . "'" )->select ();
				else if ($adminData ['name'] == C ( 'MANA_NAME' ))
					$agentDatas = $adminInfo->where ( $agentWhere . "name!='" . C ( 'ADMIN_NAME' ) . "' AND " . "name!='" . C ( 'DEVE_NAME' ) . "'" )->select ();
			}
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$code = $adminData ['code'];
			$subCode = mb_substr ( $code, 0, 5 );
			if ($agentWhere != '')
				$agentWhere .= ' AND ';
			if ($type == 0)
				$agentDatas = $adminInfo->where ( $agentWhere . "code like '" . $subCode . "___' AND level=3" )->select ();
			else
				$agentDatas = $adminInfo->where ( $agentWhere . "code like '" . $subCode . "___'" )->select ();
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		}
		if ($logWhere != '')
			$logWhere .= ' AND ';
		
		for($i = 0; $i < count ( $agentDatas ); $i ++) {
			
			$code = $agentDatas [$i] ['code'];
			$level = $agentDatas [$i] ['level'];
			if ($level == 2) {
				$subCode = mb_substr ( $code, 0, 5 );
				$userCoins = $userInfo->where ( "code like '" . $subCode . "___'" )->sum ( 'coin' );
			} else if ($level == 3) {
				$userCoins = $userInfo->where ( "code = '$code'" )->sum ( 'coin' );
			}
			
			$totalUp = $userRechargeLog->where ( $logWhere . "code='$code' AND type='0'" )->sum ( 'coins' );
			$totalDown = $userRechargeLog->where ( $logWhere . "code='$code' AND type='1'" )->sum ( 'coins' );
			$totalSend = $userRechargeLog->where ( $logWhere . "code='$code' AND type='2'" )->sum ( 'coins' );
			$totalBet = $userBetLog->where ( $logWhere . "code='$code'" )->sum ( 'bets' );
			$totalWin = $userWinLog->where ( $logWhere . "code='$code'" )->sum ( 'wins' );
			if ($totalUp == '')
				$totalUp = 0;
			else
				$totalUp = round ( $totalUp, 2 );
			if ($totalDown == '')
				$totalDown = 0;
			else
				$totalDown = round ( $totalDown, 2 );
			if ($totalSend == '')
				$totalSend = 0;
			else
				$totalSend = round ( $totalSend, 2 );
			if ($userCoins == '')
				$userCoins = 0;
			else
				$userCoins = round ( $userCoins, 2 );
			if ($totalBet == '')
				$totalBet = 0;
			else
				$totalBet = round ( $totalBet, 2 );
			if ($totalWin == '')
				$totalWin = 0;
			else
				$totalWin = round ( $totalWin, 2 );
			
			$agentDatas [$i] ['totalup'] = ( string ) $totalUp;
			$agentDatas [$i] ['totaldown'] = ( string ) $totalDown;
			$agentDatas [$i] ['totalsend'] = ( string ) $totalSend;
			$agentDatas [$i] ['totalbet'] = ( string ) $totalBet;
			$agentDatas [$i] ['totalwin'] = ( string ) $totalWin;
			$agentDatas [$i] ['usercoin'] = ( string ) $userCoins;
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $agentDatas ) );
	}
	/**
	 * 玩家账目
	 */
	public function mb_UserAccount() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		$type = $data->type;
		$userCode = $data->code;
		$userId = $data->id;
		$userName = $data->name;
		$start = $data->start;
		$end = $data->end;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		// if ($userName == '') {
		// $where = '';
		// } else {
		// $where = "name='$userName'";
		// }
		
		if ($userCode != '') {
			$where = "code='$userCode'";
		} else if ($userId != '') {
			$where = "id='$userId'";
		} else if ($userName != '') {
			$where = "name='$userName'";
		} else {
			$where = '';
		}
		
		$userInfo = M ( 'Userinfo' );
		$userRechargeLog = M ( 'Userrechargelog' );
		$userWinLog = M ( 'Userwinlog' );
		$userBetLog = M ( 'Userbetlog' );
		
		if ($adminData ['level'] == 0) {
			// 总管理员
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			$this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			if ($code == '') {
				$code = $adminData ['code'];
				$subCode = mb_substr ( $code, 0, 5 );
				if ($where != '')
					$where .= ' AND ';
				$where .= "code like '" . $subCode . "___'";
			}
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$code = $adminData ['code'];
			if ($where != '')
				$where .= ' AND ';
			$where .= "code ='$code'";
		}
		
		$userData = $userInfo->where ( $where )->select ();
		for($i = 0; $i < count ( $userData ); $i ++) {
			$name = $userData [$i] ['name'];
			if ($type == 0) {
				// 全部
				$upWhere = "username='$name' AND type='0'";
				$downWhere = "username='$name' AND type='1'";
				$sendWhere = "username='$name' AND type='2'";
			} else if ($type == 1) {
				// 代理自己上分的账目
				$code = $adminData ['code'];
				$upWhere = "username='$name' AND type='0' AND handlercode='$code'";
				$downWhere = "username='$name' AND type='1' AND handlercode='$code'";
				$sendWhere = "username='$name' AND type='2' AND handlercode='$code'";
			} else if ($type == 2) {
				// 平台上分的账目
				$upWhere = "username='$name' AND type='0' AND handlercode='1'";
				$downWhere = "username='$name' AND type='1' AND handlercode='1'";
				$sendWhere = "username='$name' AND type='2' AND handlercode='1'";
			}
			if ($start == '' && $end == '') {
				$totalUp = $userRechargeLog->where ( $upWhere )->sum ( 'coins' );
				$totalDown = $userRechargeLog->where ( $downWhere )->sum ( 'coins' );
				$totalSend = $userRechargeLog->where ( $sendWhere )->sum ( 'coins' );
				$totalBet = $userBetLog->where ( "name='$name'" )->sum ( 'bets' );
				$totalWin = $userWinLog->where ( "name='$name'" )->sum ( 'wins' );
			} else {
				$totalUp = $userRechargeLog->where ( "$upWhere AND date > '$start' AND date < '$end'" )->sum ( 'coins' );
				$totalDown = $userRechargeLog->where ( "$downWhere AND date > '$start' AND date < '$end'" )->sum ( 'coins' );
				$totalSend = $userRechargeLog->where ( "$sendWhere AND date > '$start' AND date < '$end'" )->sum ( 'coins' );
				$totalBet = $userBetLog->where ( "name='$name' AND date > '$start' AND date < '$end'" )->sum ( 'bets' );
				$totalWin = $userWinLog->where ( "name='$name' AND date > '$start' AND date < '$end'" )->sum ( 'wins' );
			}
			
			if ($totalUp == '')
				$totalUp = 0;
			else
				$totalUp = round ( $totalUp, 3 );
			if ($totalDown == '')
				$totalDown = 0;
			else
				$totalDown = round ( $totalDown, 3 );
			if ($totalSend == '')
				$totalSend = 0;
			else
				$totalSend = round ( $totalSend, 3 );
			if ($totalBet == '')
				$totalBet = 0;
			else
				$totalBet = round ( $totalBet, 3 );
			if ($totalWin == '')
				$totalWin = 0;
			else
				$totalWin = round ( $totalWin, 3 );
			
			$userData [$i] ['totalup'] = ( string ) $totalUp;
			$userData [$i] ['totaldown'] = ( string ) $totalDown;
			$userData [$i] ['totalsend'] = ( string ) $totalSend;
			$userData [$i] ['totalbet'] = ( string ) $totalBet;
			$userData [$i] ['totalwin'] = ( string ) $totalWin;
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	}
	/**
	 * 充值明细
	 */
	public function mb_RechargeDetail() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$userName = $data->name;
		$userId = $data->id;
		$date1 = $data->date1;
		$date2 = $data->date2;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		
		$needCheckID = true;
		$userInfo = M ( 'Userinfo' );
		if ($userName != '') {
			$where = "username='$userName'";
			
			$userData = $userInfo->where ( "name='$userName'" )->find ();
			$userId = $userData ['id'];
			$needCheckID = false;
		} else if ($userId != '') {
			
			$userData = $userInfo->where ( "id='$userId'" )->find ();
			if (isset ( $userData ) == false) {
				$this->mb_OutMessage ( ErrCode_Toast, '该玩家ID无效' );
				return;
			}
			$userName = $userData ['name'];
			$where = "username='$userName'";
			$needCheckID = false;
		} else if ($date1 != '' && $date2 != '') {
			$where = "date>='$date1' AND date<='$date2'";
			$needCheckID = true;
		} else {
			$where = '';
			$needCheckID = true;
		}
		
		$userRechargeLog = M ( 'userrechargelog' );
		if ($adminData ['level'] == 0) {
			// 总管理员
			// if ($where != '')
			// $where .= ' AND ';
			// $handlerName = $adminData ['name'];
			// $where .= "handlername ='$handlerName'";
			$aName = C ( 'ADMIN_NAME' );
			$dName = C ( 'DEVE_NAME' );
			$mName = C ( 'MANA_NAME' );
			if ($adminData ['name'] == $aName) {
			} else if ($adminData ['name'] == $dName) {
				if ($where != '')
					$where .= ' AND ';
				$where .= "handlername !='$aName'";
			} else if ($adminData ['name'] == $mName) {
				if ($where != '')
					$where .= ' AND ';
				$where .= "handlername !='$aName' AND handlername != '$dName'";
			}
		} else if ($adminData ['level'] == 1) {
			// 平台上分
			// $this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
			// return;
			// if ($where != '')
			// $where .= ' AND ';
			// $handlerName = $adminData ['name'];
			// $where .= "handlername ='$handlerName'";
			$aName = C ( 'ADMIN_NAME' );
			$dName = C ( 'DEVE_NAME' );
			$mName = C ( 'MANA_NAME' );
			if ($where != '')
				$where .= ' AND ';
			$where .= "handlername !='$aName' AND handlername != '$dName' AND handlername != '$mName'";
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			// $code = $adminData ['code'];
			// $subCode = mb_substr ( $code, 0, 5 );
			// if ($where != '')
			// $where .= ' AND ';
			// // $where .= "code like '" . $subCode . "___'";
			// $where .= "code ='$code'";
			if ($where != '')
				$where .= ' AND ';
			$handlerName = $adminData ['name'];
			$where .= "handlername ='$handlerName'";
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			// $code = $adminData ['code'];
			// if ($where != '')
			// $where .= ' AND ';
			// $where .= "code ='$code'";
			if ($where != '')
				$where .= ' AND ';
			$handlerName = $adminData ['name'];
			$where .= "handlername ='$handlerName'";
		}
		$logDatas = $userRechargeLog->where ( $where )->order ( 'id desc' )->select ();
		
		for($i = 0; $i < count ( $logDatas ); $i ++) {
			if ($needCheckID) {
				$userName = $logDatas [$i] ['username'];
				$userData = $userInfo->where ( "name='$userName'" )->find ();
				$logDatas [$i] ['id'] = $userData ['id'];
			} else {
				$logDatas [$i] ['id'] = $userId;
			}
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $logDatas ) );
	}
	
	/**
	 */
	public function mb_RechargeDetailForOther() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$type = $data->type;
		$value = $data->value;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		$userRechargeLog = M ( 'userrechargelog' );
		
		$userInfo = M ( 'Userinfo' );
		
		if ($type == 0) {
			// 自己操作的
			$handlercode = $adminData ['code'];
			$handlername = $adminData ['name'];
			// $where = "handlercode = '$handlercode'";
			$where = "handlername = '$handlername'";
		} else if ($type == 1) {
			// 全部充值记录
			if ($adminData ['level'] == 0) {
				// 总管理员
				// $where = '';
				$aName = C ( 'ADMIN_NAME' );
				$dName = C ( 'DEVE_NAME' );
				$mName = C ( 'MANA_NAME' );
				if ($adminData ['name'] == $aName) {
				} else if ($adminData ['name'] == $dName) {
					if ($where != '')
						$where .= ' AND ';
					$where .= "handlername !='$aName'";
				} else if ($adminData ['name'] == $mName) {
					if ($where != '')
						$where .= ' AND ';
					$where .= "handlername !='$aName' AND handlername != '$dName'";
				}
			} else if ($adminData ['level'] == 1) {
				// 平台上分
				// $this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
				// return;
				// $where = '';
				$aName = C ( 'ADMIN_NAME' );
				$dName = C ( 'DEVE_NAME' );
				$mName = C ( 'MANA_NAME' );
				if ($where != '')
					$where .= ' AND ';
				$where .= "handlername !='$aName' AND handlername != '$dName' AND handlername != '$mName'";
			} else if ($adminData ['level'] == 2) {
				// 一级代理
				$code = $adminData ['code'];
				$subCode = mb_substr ( $code, 0, 5 );
				$where .= "code like '" . $subCode . "___'";
			} else if ($adminData ['level'] == 3) {
				// 二级代理
				$code = $adminData ['code'];
				$where .= "code ='$code'";
			}
		} else if ($type == 2) {
			// 按操作员
			$where = "handlercode = '$value'";
		}
		if ($where == '')
			$logDatas = $userRechargeLog->order ( 'id desc' )->select ();
		else
			$logDatas = $userRechargeLog->where ( $where )->order ( 'id desc' )->select ();
		
		for($i = 0; $i < count ( $logDatas ); $i ++) {
			$userName = $logDatas [$i] ['username'];
			$userData = $userInfo->where ( "name='$userName'" )->find ();
			$logDatas [$i] ['id'] = $userData ['id'];
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $logDatas ) );
	}
	public function mb_HandlerAccount() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		$adminName = $adminData ['name'];
		$userInfo = M ( 'Userinfo' );
		if ($adminData ['level'] == 0) {
			$userData = $userInfo->select ();
		} else if ($adminData ['level'] == 1) {
			$userData = $userInfo->select ();
		} else if ($adminData ['level'] == 2) {
			$code = mb_substr ( $adminData ['code'], 0, 5 );
			$userData = $userInfo->where ( "code='$code" . "___'" )->select ();
		} else if ($adminData ['level'] == 3) {
			$code = $adminData ['code'];
			$userData = $userInfo->where ( "code='$code'" )->select ();
		}
		$sendData = array ();
		$userRechargeLog = M ( 'Userrechargelog' );
		for($i = 0; $i < count ( $userData ); $i ++) {
			$userName = $userData [$i] ['name'];
			$up = $userRechargeLog->where ( "username='$userName' AND handlername='$adminName' AND type='0'" )->sum ( 'coins' );
			$down = $userRechargeLog->where ( "username='$userName' AND handlername='$adminName' AND type='1'" )->sum ( 'coins' );
			$send = $userRechargeLog->where ( "username='$userName' AND handlername='$adminName' AND type='2'" )->sum ( 'coins' );
			
			if ($up == '' && $down == '' && $send == '')
				continue;
			
			if ($up == '')
				$up = 0;
			else
				$up = round ( $up, 2 );
			
			if ($down == '')
				$down = 0;
			else
				$down = round ( $down, 2 );
			
			if ($send == '')
				$send = 0;
			else
				$send = round ( $send, 2 );
			$userData [$i] ['up'] = ( double ) $up;
			$userData [$i] ['down'] = ( double ) $down;
			$userData [$i] ['send'] = ( double ) $send;
			$sendData [] = $userData [$i];
		}
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( $sendData ) );
	}
	/**
	 * 获取月结账单
	 */
	public function mb_GetMonthlyStatement() {
		$data = $this->mb_OnMessage ();
		$token = $data->token;
		
		$ret = $this->mb_CheckAdminIsLoggedIn ( $token );
		$adminInfo = $ret->info;
		$adminData = $ret->data;
		$adminCode = $adminData ['code'];
		
		$start = date ( 'Y-m-01', strtotime ( '-1 month' ) );
		$end = date ( 'Y-m-t', strtotime ( '-1 month' ) );
		
		$table = M ( 'Userstatisticdata' );
		if ($adminData ['level'] == 0) {
			// 一级代理
			$data = $table->select ();
		} else if ($adminData ['level'] == 1) {
			// 平台上分员
			$this->mb_OutMessage ( ErrCode_Toast, '您暂无权限' );
			return;
		} else if ($adminData ['level'] == 2) {
			// 一级代理
			$subCode = mb_substr ( $adminCode, 0, 5 );
			$data = $table->where ( "code like '" . $subCode . "___'" )->select ();
		} else if ($adminData ['level'] == 3) {
			// 二级代理
			$data = $table->where ( "code='$adminCode'" )->select ();
		}
		
		$this->mb_OutMessage ( ErrCode_Normal, json_encode ( array (
				'start' => $start,
				'end' => $end,
				'data' => $data 
		) ) );
	}
	
	// /**
	// * 代理上分账目
	// */
	// public function mb_AgentUpAccount() {
	// $data = $this->mb_OnMessage ();
	// $token = $data->token;
	// $userName = $data->name;
	
	// $ret = $this->mb_CheckAdminIsLoggedIn ( $token );
	// $adminInfo = $ret->info;
	// $adminData = $ret->data;
	
	// if ($userName == '') {
	// $where = '';
	// } else {
	// $where = "name='$userName'";
	// }
	
	// $userInfo = M ( 'Userinfo' );
	// $userRechargeLog = M ( 'Userrechargelog' );
	// $userWinLog = M ( 'Userwinlog' );
	// $userBetLog = M ( 'Userbetlog' );
	
	// if ($adminData ['level'] == 0) {
	// // 总管理员
	// } else if ($adminData ['level'] == 1) {
	// // 平台上分
	// $this->mb_OutMessage ( ErrCode_Toast, '您没有该权限' );
	// return;
	// } else if ($adminData ['level'] == 2) {
	// // 一级代理
	// $code = $adminData ['code'];
	// $subCode = mb_substr ( $code, 0, 5 );
	// if ($where != '')
	// $where .= ' AND ';
	// $where .= "code like '" . $subCode . "___'";
	// } else if ($adminData ['level'] == 3) {
	// // 二级代理
	// $code = $adminData ['code'];
	// if ($where != '')
	// $where .= ' AND ';
	// $where .= "code ='$code'";
	// }
	
	// $userData = $userInfo->where ( $where )->select ();
	// for($i = 0; $i < count ( $userData ); $i ++) {
	// $name = $userData [$i] ['name'];
	// // $userData ['coin'] = round ( $userData ['coin'], 2 );
	// $totalUp = $userRechargeLog->where ( "username='$name' AND type='0' " )->sum ( 'coins' );
	// $totalDown = $userRechargeLog->where ( "username='$name' AND type='1'" )->sum ( 'coins' );
	// $totalSend = $userRechargeLog->where ( "username='$name' AND type='2'" )->sum ( 'coins' );
	// $totalBet = $userBetLog->where ( "name='$name'" )->sum ( 'bets' );
	// $totalWin = $userWinLog->where ( "name='$name'" )->sum ( 'wins' );
	
	// if ($totalUp == '')
	// $totalUp = 0;
	// else
	// $totalUp = round ( $totalUp, 2 );
	// if ($totalDown == '')
	// $totalDown = 0;
	// else
	// $totalDown = round ( $totalDown, 2 );
	// if ($totalSend == '')
	// $totalSend = 0;
	// else
	// $totalSend = round ( $totalSend, 2 );
	// if ($totalBet == '')
	// $totalBet = 0;
	// else
	// $totalBet = round ( $totalBet );
	// if ($totalWin == '')
	// $totalWin = 0;
	// else
	// $totalWin = round ( $totalWin, 2 );
	
	// $userData [$i] ['totalup'] = ( string ) $totalUp;
	// $userData [$i] ['totaldown'] = ( string ) $totalDown;
	// $userData [$i] ['totalsend'] = ( string ) $totalSend;
	// $userData [$i] ['totalbet'] = ( string ) $totalBet;
	// $userData [$i] ['totalwin'] = ( string ) $totalWin;
	// }
	// $this->mb_OutMessage ( ErrCode_Normal, json_encode ( $userData ) );
	// }
}